package com.wangtao.firstapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.AllEssayAdapter;
import com.wangtao.firstapp.custom.FullyLinearLayoutManager;
import com.wangtao.firstapp.modles.AllEssayData;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * 全部文章页面
 * Created by lanouhn on 16/9/14.
 */
public class AllEssayActivity extends Activity {

    private ImageButton back;
    private RecyclerView allessay_RecyclerView;

    private AllEssayData allEssayData;
    private AllEssayAdapter allEssayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_allessay);
        initView();
        getDataFromWeb();
        initEvent();
    }


    private void initView() {
        back = (ImageButton) findViewById(R.id.back_act_allessay);//左上角返回
        allessay_RecyclerView = (RecyclerView) findViewById(R.id.allessay_act_allessay);//容器
    }

    private void getDataFromWeb() {
        Intent intent = getIntent();
        String essay_ID = intent.getStringExtra("essay_iD");

        String urlEssay = "http://open4.bantangapp.com/products/brandProduct/relationArticleTopicList";
        String paramsEssay = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page=0&pagesize=10&id=" + essay_ID;

        try {
            HttpUtils.doPostAsyn(urlEssay, paramsEssay, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_AllEssay = new Gson();
                    allEssayData = gson_AllEssay.fromJson(result, AllEssayData.class);
                    if (allEssayData.getData().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    allEssayAdapter=new AllEssayAdapter(AllEssayActivity.this,allEssayData);
                    allessay_RecyclerView.setAdapter(allEssayAdapter);
                    allessay_RecyclerView.setLayoutManager(new FullyLinearLayoutManager(AllEssayActivity.this));
                    break;
            }

        }
    };

    private void initEvent() {
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
    }

}
